<template>
  <div class="add-worker">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回负责人</span>
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="contain">添加负责人</div>
        <div class="form">
          <el-form ref="ruleForm" :model="addForm" :rules="addRules" label-width="100px">
            <el-form-item label="管道编号" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
              </el-select>
            </el-form-item>
            <el-form-item label="负责人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button type="primary" @click="confirmAdd">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import {createWorkerListAPI} from '@/api/worker'
export default {
  data() {
    const validatePhone = (rule, value, cb) => {
      if (/^1[3-9]\d{9}$/.test(value)) {
        cb()
      } else {
        cb(new Error('请输入正确联系方式'))
      }
    }
    return {
      addForm: {
        name: '', // 管道编号
        contact: '', // 负责人
        contactNumber: '', // 联系电话
      },
      addRules: {
        name: [
          { required: true, message: '请输入管道编号', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入负责人名称', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    confirmAdd() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // TODO
          await createWorkerListAPI(this.addForm)
          this.$message.success('添加成功!')
          this.$router.back()
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.add-worker {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-left: 4px;
        font-size: 16px;
      }
      .arrow{
        cursor: pointer;
      }
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .contain {
        height: 100px;
        line-height: 80px;
        padding-left:40px;
        font-size: 18px;
      }
      .form {
        margin-bottom: 80px;
        padding: 20px 365px 34px;
        .el-form{
          display: flex;
          display: block;
          flex-wrap: wrap;
          .el-form-item{
            height: 100px;
            width: 80%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
  .img{
    width: 150px;
    height: 150px;
    border-radius: 4px;
  }
}
</style>
